package server;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import GUI.ResultDisplay;

import logic.Book;
import logic.Member;
import logic.Message;
import ocsf.server.ConnectionToClient;


public class WorkerController {

	private DB_Manager DB;
	private OBL_Server parent;
	
	public WorkerController(OBL_Server parent,DB_Manager db) {
		this.parent = parent;
		this.DB = db;
	}
	
	
	/**
	 * This function will search the DB for member by the requested fields
	 * The Result will be sent as a Message to the client
	 * @param received - the Message object containing the fields and their requested values
	 * @param client - the client which requested the search
	 */
	public void SearchMember(Message received, ConnectionToClient client){
		
		//ResultSet rs = QueryMember(received.GetMessageIndexes(), received.GetMessageData());
		
		//ArrayList<Member> memberList = SeralizeResultSet(rs);
		
		ArrayList<String> index = new ArrayList<String>();
		index.add("INDICATOR");
		index.add("RESULTSET");
		index.add("DIRECTOR");
		ArrayList<Object> data = new ArrayList<Object>();
		data.add(ResultDisplay.MEMBER);
		//data.add(memberList);
		data.add(received.GetMessageData().get(0));
		
		int action = Message.SEARCH_MEMBER_RESPONSE;
		
		Message toSend = new Message(index, data, action);
		
		try{
			client.sendToClient(toSend);
		}catch(IOException e){
			//TODO handle this error
		}	
	}
	
	
	
	/**
	 * This Function convert a ResultSet into ArrayList<Member> for sending as a Message
	 * (ResultSet is not seralizeable)
	 * @param rs - the ResultSet to conver
	 * @return ArrayList<Member> which conatins all the members in the ResultSet
	 */
	public ArrayList<Member> SeralizeResultSet(ResultSet rs){
		
		ArrayList<Member> memberList = new ArrayList<Member>();
		
		try{
			while(rs.next()){	
				//Member(int UID,String name, String dob, String phone, String mail, String address,int status)
				Member member = new Member(rs.getInt("UID"), rs.getString("NAME"), rs.getString("DOB"), 
						rs.getString("PHONE"), rs.getString("MAIL"), rs.getString("ADDRESS"),rs.getInt("STATUS"));
				memberList.add(member);
			}
			
			return memberList;
		}catch(SQLException e){
			//TODO handle seralize error
			return null;
		}
	}
	
	
	/**
	 * This function is meant to query the actual DB for the fields and return the ResultSet
	 * @param fields - the fields to query by
	 * @param data - the data to query by
	 * @return ResultSet which contains all qualified tuples
	 */
	public ResultSet QueryWorker(ArrayList<String> fields,ArrayList<Object> data){
		
		String query = "SELECT * FROM users WHERE PRIVILIGE = 100";
		if (fields.size() > 1) {
			String field;
			for(int i=1; i < fields.size(); i++){
				field = (String)fields.get(i);
				if ((field.equals("DOB")) || (field.equals("MAIL"))) query += " AND " + field + " = '" + (String)data.get(i) + "'";
				else query += " AND " + field + " = '" + (String)data.get(i) + "'";
			}
			query += ";";
		}
		
		return DB.Query(query);
	}
}
